package com.woniu.springboot.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.woniu.springboot.dao.StudentDao;
import com.woniu.springboot.entity.Student;

@Repository
public class StudentDaoImpl implements StudentDao {
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public void insertStudent(Student student) {
		entityManager.persist(student);
		// throw new RuntimeException();
	}
	@SuppressWarnings("all")
	@Override
	public List<Student> selecttStudents() {
		String hql = "from Student";
		return (List<Student>) entityManager.createQuery(hql).getResultList();
	}
	
	@Override
	public Student selectStudentBySid(Integer sid) {
		String sql = "select * from student where sid = "+sid;
		return (Student) entityManager.createNativeQuery(sql, Student.class)
				.getSingleResult();// entityManager.find(Student.class, sid);
	}

	@Override
	public void deleteStudentBySid(Integer sid) {	
		Student student = selectStudentBySid(sid);
		entityManager.remove(student);
	}
	@Override
	public void updateStudentBySid(Student student) {
		Student selectStudentBySid = selectStudentBySid(student.getSid());
		selectStudentBySid.setSname(student.getSname());
		selectStudentBySid.setAge(student.getAge());
		selectStudentBySid.setTeacher(student.getTeachers());
	}


	


}
